﻿using System;
using System.Web.Mvc;
using Epam.Exceptions.BusinessLogicExceptions;
using Epam.PeopleInterfaces;
using Epam.PeopleModel;

namespace PeopleListTask.Controllers
{
    public class NewsController : Controller
    {
        private readonly INewsService _newsService;

        private readonly INewsJsonService _newsJsonService;
        
        public NewsController(INewsService newsService,INewsJsonService newsJsonService)
        {
            _newsService = newsService;
            _newsJsonService = newsJsonService;
        }
        //
        // GET: /News/
        
        public ActionResult NewsListPanel()
        {
            try
            {
                var list = _newsService.ReadAllView();
                return PartialView(list);
            }
            catch (DatabaseException e)
            {
                ViewData["errorMessage"] = e.Message;
                return View("Error");
            }
           
        }

        public ActionResult AdminNewsList()
        {
            try
            {
                var list = _newsService.ReadAll();
                return View(list);
            }
            catch (DatabaseException e)
            {
                ViewData["errorMessage"] = e.Message;
                return View("Error");
            }
           
        }

        [HttpPost]
        public ActionResult Create(NewsEntity entity)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    _newsService.Create(entity);
                    return Json(new {data = entity, success = true}, JsonRequestBehavior.AllowGet);
                }
                return Json(new {success = false}, JsonRequestBehavior.AllowGet);
            }
            catch (DatabaseException)
            {
                return Json(new { success = false }, JsonRequestBehavior.AllowGet);
            }
          
        }
        
        [HttpPost]
        public ActionResult Delete(Guid id)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    _newsService.Delete(id);
                    return Json(new {success=true},JsonRequestBehavior.AllowGet);
                }
                return Json(new { success = false }, JsonRequestBehavior.AllowGet);
            }
            catch (DatabaseException)
            {
                return Json(new { success = false }, JsonRequestBehavior.AllowGet);
            }
         
        }

        [HttpPost]
        public ActionResult Update(NewsEntity entity)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    _newsService.Update(entity);
                    return Json(new { success = true }, JsonRequestBehavior.AllowGet);
                }
                return Json(new { success = false }, JsonRequestBehavior.AllowGet);
            }
            catch (DatabaseException)
            {
                return Json(new { success = false }, JsonRequestBehavior.AllowGet);
            }
          
        }

        public ActionResult MoreInformation(Guid id)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    return View(_newsService.GetById(id));
                }
                return View();
            }
            catch (DatabaseException e)
            {
                ViewData["errorMessage"] = e.Message;
                return View("Error");
            }
           
        }

        public ActionResult GetJsonNews()
        {
            return Json(_newsJsonService.Convert(_newsService.ReadAll()),JsonRequestBehavior.AllowGet);
        }
    }
}
